package com.glela.common.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * 文件操作类
 *
 * @author liujie
 *
 */
public class FileUtil {

	/**
	 * 获取文件列表
	 *
	 * @param dir
	 * @return
	 */
	public static List<File> getFileList(String dir) {

		File dirPath = new File(dir);
		File[] files = dirPath.listFiles();
		if ((files != null) && (files.length > 0)) {
			List<File> fileList = Arrays.asList(files);
			Collections.sort(fileList);
			return fileList;
		}
		return null;
	}

	/**
	 * <dl>
	 * 说明：上传文件
	 * <dt></dt>
	 * <dd>后台根据fileResources获取文件对象；根据channelSourceType获取文件来源</dd>
	 * </dl>
	 * @author liujinlei
	 * @date 2016年1月4日 下午2:15:29
	 * @Title:postFile
	 * @param url 上传地址
	 * @param targetFile 上传文件对象
	 * @param channelSourceType 文件来源
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException String
	 */
	public static String postFile(String url, File targetFile, String channelSourceType) throws ClientProtocolException, IOException {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("channelSourceType", channelSourceType);
		return postFile(url, "fileResources", targetFile, params);
	}

	/**
	 * <dl>
	 * 说明：文件上传基础方法
	 * <dt></dt>
	 * <dd></dd>
	 * </dl>
	 * @author liujinlei
	 * @date 2016年1月4日 下午2:42:10
	 * @Title:postFile
	 * @param url 上传文件地址
	 * @param fileTargetName 上传文件标识
	 * @param targetFile 上传文件
	 * @param params 文件参数
	 * @return String
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String postFile(String url, String fileTargetName, File targetFile, Map<String, Object> params) throws ClientProtocolException, IOException {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		try {
			if (targetFile == null || !targetFile.exists()) {
				throw new FileNotFoundException(fileTargetName + " is null");
			}
			if (!targetFile.isFile()) {
				throw new FileNotFoundException(fileTargetName + " is not file.");
			}

			HttpPost httppost = new HttpPost(url);

			FileBody bin = new FileBody(targetFile);
			MultipartEntityBuilder addPart = MultipartEntityBuilder.create().addPart(fileTargetName, bin);
			for (String key : params.keySet()) {
				if (params.get(key) != null) {
					addPart.addTextBody(key, params.get(key).toString(), ContentType.TEXT_PLAIN);
				}
			}
			httppost.setEntity(addPart.build());

			CloseableHttpResponse response = httpclient.execute(httppost);
			try {
				HttpEntity resEntity = response.getEntity();
				String responseBody = null;
				if (resEntity != null) {
					responseBody = EntityUtils.toString(resEntity);
				}
				EntityUtils.consume(resEntity);
				return responseBody;
			} finally {
				response.close();
				targetFile = null;
			}
		} finally {
			httpclient.close();
		}
	}

	public static void main(String[] args) {
		//		String str = "1@0@512093@-1@114.3453@36.2345@20@0000";
		//		String arr[] = str.split("@");
		//		System.out.println(arr[2]);
	}
}
